<?php
/**
 * 设置
 */
class SettingController extends AppController {
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 是否登陆
		if (is_null ( $this->Cookie->read ( 'uid' ) ) || $this->Cookie->read ( 'uid' ) == 0) {
			$this->redirect ( "/login/" );
		}
	}
	
	/**
	 * 基本信息
	 */
	public function index() {
		if ($this->request->isPost ()) {
			$sex = $this->data ['sex'];
			if (isset ( $sex )) {
				$data ['User'] ['sex'] = $sex;
			}
			$province = $this->data ['province'];
			if (! empty ( $province )) {
				$data ['User'] ['province'] = $province;
			}
			$city = $this->data ['city'];
			if (! empty ( $city )) {
				$data ['User'] ['city'] = $city;
			}
			$area = $this->data ['area'];
			if (! empty ( $area )) {
				$data ['User'] ['area'] = $area;
			}
			
			$birthday = '';
			$y = $this->data ['y'];
			if (empty ( $y )) {
				$birthday .= '0000-';
			} else {
				$birthday .= "$y-";
			}
			$m = $this->data ['m'];
			if (empty ( $m )) {
				$birthday .= '00-';
			} else {
				$birthday .= "$m-";
			}
			$d = $this->data ['d'];
			if (empty ( $d )) {
				$birthday .= '00';
			} else {
				$birthday .= "$d";
			}
			$data ['User'] ['birthday'] = $birthday;
			
			$qq = $this->data ['qq'];
			if (! empty ( $qq )) {
				$data ['User'] ['qq'] = $qq;
			}
			$desc = $this->data ['desc'];
			if (! empty ( $desc )) {
				$data ['User'] ['desc'] = $desc;
			}
			$data ['User'] ['uid'] = $this->Cookie->read ( 'uid' );
			
			$this->loadModel ( "User" );
			if ($this->User->edit ( $data )) {
				$this->flash ( '基本信息修改成功!', '/setting/', 1250 );
				return false;
			} else {
				$this->flash ( '基本信息修改失败!', '/setting/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '女人集市 - 基本信息' );
			
			// 加载css文件
			$this->set ( 'cssList', array (
					'setting.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'setting.js',
					'city.js' 
			) );
			
			$this->loadModel ( "User" );
			$UserInfo = $this->User->GetUserByID ( $this->Cookie->read ( 'uid' ) );
			$birthday = explode ( '-', $UserInfo ['User'] ['birthdate'] );
			$UserInfo ['User'] ['y'] = $birthday [0];
			$UserInfo ['User'] ['m'] = $birthday [1];
			$UserInfo ['User'] ['d'] = $birthday [2];
			$this->set ( "UserInfo", $UserInfo );
			
			$this->loadModel ( "City" );
			// 省
			$conditions = array (
					'pid' => 0 
			);
			$ProvinceList = $this->City->GetCityByList ( $conditions );
			$this->set ( "ProvinceList", $ProvinceList );
			
			// 市
			$conditions = array (
					'pid' => $UserInfo ['User'] ['province'] 
			);
			$CityList = $this->City->GetCityByList ( $conditions );
			$this->set ( "CityList", $CityList );
			
			// 区
			$conditions = array (
					'pid' => $UserInfo ['User'] ['city'] 
			);
			$AreaList = $this->City->GetCityByList ( $conditions );
			$this->set ( "AreaList", $AreaList );
		}
	}
	
	/**
	 * 修改头像
	 */
	public function avatar() {
		$this->set ( 'title_for_layout', '女人集市 - 修改头像' );
	}
	
	/**
	 * 修改密码
	 */
	public function password() {
		if ($this->request->isPost ()) {
			$password = $this->data ['password'];
			if (empty ( $password )) {
				$this->flash ( '请输入原密码!', '/password/', 1250 );
				return false;
			}
			
			// 验证原密码是否正确
			$this->loadModel ( "User" );
			$UserInfo = $this->User->GetUserByID ( $this->Cookie->read ( 'uid' ) );
			if ($UserInfo ['User'] ['password'] != md5 ( $password )) {
				$this->flash ( '原密码错误!', '/setting/password/', 1250 );
				return false;
			}
			
			$newpassword = $this->data ['newpassword'];
			if (empty ( $newpassword )) {
				$this->flash ( '请输入新密码!', '/setting/password/', 1250 );
				return false;
			}
			$confirmnewpassword = $this->data ['confirmnewpassword'];
			if (empty ( $confirmnewpassword )) {
				$this->flash ( '请输入确认新密码!', '/setting/password/', 1250 );
				return false;
			}
			
			if ($newpassword != $confirmnewpassword) {
				$this->flash ( '请确认新密码是否正确!', '/setting/password/', 1250 );
				return false;
			}
			
			$data ['User'] ['password'] = md5 ( $confirmnewpassword );
			$data ['User'] ['uid'] = $this->Cookie->read ( 'uid' );
			
			if ($this->User->edit ( $data )) {
				$this->flash ( '密码修改成功!', '/setting/password/', 1250 );
				return false;
			} else {
				$this->flash ( '密码修改失败!', '/setting/password/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '女人集市 - 修改密码' );
			
			// 加载css文件
			$this->set ( 'cssList', array (
					'setting.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'setting.js' 
			) );
		}
	}
	
	/**
	 * 邀请码
	 */
	public function invite() {
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'setting.css' 
		) );
		
		$this->set ( 'title_for_layout', '女人集市 - 邀请码' );
	}
}
?>